package com.edu.edu.day25_homework.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.edu.edu.day25_homework.Contans;
import com.edu.edu.day25_homework.adapter.MoviesAdapter;
import com.edu.edu.day25_homework.R;
import com.edu.edu.day25_homework.bean.Movies;
import com.edu.edu.day25_homework.thread.loadJsonTask;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ShowFragmen extends Fragment implements loadJsonTask.onGetJsonResultListener{


    private ListView mLv;
    private ViewPager mVp;
    private List<ImageView> images;
    private MoviesAdapter adapter;
    private int[] imagesId;

    public ShowFragmen() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_show, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLv = (ListView)view.findViewById(R.id.show_list);
        mVp = (ViewPager)view.findViewById(R.id.show_vp);

        initViewData();

        initListData();
    }

    private void initListData() {

        new loadJsonTask(this).execute(Contans.JSON_WILLSHOW);

        assert mLv != null;

        adapter = new MoviesAdapter(getActivity());

        mLv.setAdapter(adapter);
    }
    @Override
    public void onGetJsonResult(List<Movies> result) {
        adapter.setData(result);

    }

    private void initViewData() {
        images = new ArrayList<>();
        imagesId = getImagesId();
        for (int i = 0; i < imagesId.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setBackgroundResource(imagesId[i]);
            images.add(image);
        }

        mVp.setCurrentItem(0);
        assert mVp != null;
        mVp.setAdapter(new MyShowAdapter());
        assert mVp != null;
        mVp.setCurrentItem(100);

        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
        });
    }
        class MyShowAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position  % imagesId.length));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position % imagesId.length));
            return images.get(position % imagesId.length);
        }
    }

    public int[] getImagesId() {
        return new int[]{R.mipmap.image09,R.mipmap.image10,R.mipmap.image11};
    }
}
